package cn.zhm.face;

import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 *
 * @author ZhongHaoMing
 * @emali: 3504677638@qq.com
 * @since: 1.8
 * @date 2021-12-02  11:37
 */
public class Test {
    private static int fps = 0;
    private static int fps1 = 0;


    public static void main(String[] args) {
        p(999, () -> {
            fps1 = fps;
            fps = 0;
        });
        testCamera();

    }
    public static void p(long time, Runnable r) {
        new Thread(() -> {
            try {
                while (true) {
                    Thread.sleep(time);
                    r.run();
                }
            } catch (Exception e) {

            }

        }).start();
    }

    public static void testCamera(){

        int w = 640;
        int h = 380;
        // 使用摄像头驱动的第0个
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        // 水平旋转 图片处理
        Java2DFrameConverter java2dFrameConverter = new Java2DFrameConverter();
        // 设置宽高
        grabber.setImageWidth(w);
        grabber.setImageHeight(h);
        try {
            grabber.start();   //开始获取摄像头数据
            CanvasFrame canvas = new CanvasFrame("摄像头");//新建一个窗口
            p(1, () -> {
                try {
                    fps++;
                    Frame frame1 = grabber.grab(); // 获取一张图片 一帧
                    // 获取人脸位置
                    Frame frame = CheckFacePoints.dealTheMat(frame1);
                    // 转为image类型实现水平旋转
                    BufferedImage bufferedImage = java2dFrameConverter.convert(frame1);
                    Graphics graphics = bufferedImage.getGraphics();
                    graphics.drawImage(bufferedImage, 0, 0, w, h, w, 0, 0, h, null);
                    // 显示帧数
                    graphics.drawString("F P S : " + fps1, 10, 30);
                    canvas.showImage(bufferedImage);//获取摄像头图像并放到窗口上显示， 这里的Frame frame=grabber.grab(); frame是一帧视频图像

                } catch (Exception e) {

                }
            });
        }catch (Exception e){

        }



    }
}
